package com.kzd.btgame.main.mine

import android.view.LayoutInflater
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.LifecycleOwner
import androidx.viewpager.widget.ViewPager
import com.kzd.btgame.R
import com.kzd.btgame.app.SPSConstance.userInfo
import com.kzd.btgame.base.BaseViewModel
import com.kzd.btgame.callback.AppPagerAdapter
import com.kzd.btgame.custom.setOnClickAntiShakeListener
import com.kzd.btgame.databinding.ActivityMineMemberBinding
import com.kzd.btgame.main.KEY_MEMBER_INDEX
import com.kzd.btgame.main.toMineRecordBillActivity
import com.module.lemlin.app.OwnerStatusBar
import com.module.lemlin.http.Status
import com.module.lemlin.image.loadCircleImage
import com.module.lemlin.owner.OwnerViewBindingActivity
import com.module.lemlin.owner.ownerViewModel
import java.lang.Exception

class MineMemberActivity : OwnerViewBindingActivity<ActivityMineMemberBinding>() {

    companion object {
        private val tabs = arrayOf("省钱卡", "会员中心")
    }

    private val mAppPagerAdapter: AppPagerAdapter by lazy {
        object : AppPagerAdapter(supportFragmentManager, tabs) {
            override fun fragment(position: Int): Fragment = if (position == 0)
                MemberMoneyFragment.newInstance() else MemberCanterFragment.newInstance()
        }
    }

    private val mIndex: Int
        get() = try {
            (intent?.extras?.getString(KEY_MEMBER_INDEX) ?: "0").toInt()
        } catch (e: Exception) {
            0
        }

    private val mViewModel: BaseViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater) -> ActivityMineMemberBinding
        get() = ActivityMineMemberBinding::inflate

    override fun initStatusBar(statusBar: OwnerStatusBar) {
        statusBar.run {
            addView = mViewBinding.flMineMemberBar
            barColor = R.color.FF_2366E4
            lightMode = false
        }
        super.initStatusBar(statusBar)
    }

    override fun initView() {

        fillData()

        mViewBinding.vpMineMemberBody.addOnPageChangeListener(object :
            ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int, positionOffset: Float, positionOffsetPixels: Int,
            ) = Unit

            override fun onPageSelected(position: Int) {
                fillData(position)
            }

            override fun onPageScrollStateChanged(state: Int) = Unit
        })

        mViewBinding.tabMineMemberKind.apply {
            for (item in tabs) {
                addTab(newTab().setText(item))
            }
        }.setupWithViewPager(mViewBinding.vpMineMemberBody.apply {
            adapter = mAppPagerAdapter
        })
        mViewBinding.tabMineMemberKind.getTabAt(mIndex)?.select()

        mViewBinding.tvMineMemberRecord.setOnClickAntiShakeListener {
            toMineRecordBillActivity()
        }
    }

    override fun onResume() {
        super.onResume()
        mViewModel.userInfo()
    }

    override fun initViewMode() {
        mViewModel.userInfoLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            fillData()
        })
    }

    private fun fillData(position: Int = mIndex) {

        mViewBinding.ivMineMemberUserHeader.loadCircleImage(
            if (userInfo?.face.isNullOrEmpty())
                R.mipmap.ic_test_1 else userInfo?.face ?: R.mipmap.ic_test_1
        )
        mViewBinding.tvMineMemberUserName.text = if (userInfo?.pet_name.isNullOrEmpty())
            userInfo?.user_name else userInfo?.pet_name

        mViewBinding.tvMineMemberTime.text = if (position == 0) {
            if (userInfo?.is_saving ?: 0 == 1) {
                "剩余${userInfo?.saving_day}天到期"
            } else {
                "暂未购买"
            }
        } else {
            if (userInfo?.is_huiyuan ?: 0 == 1) {
                "${userInfo?.huiyuan_time}\t到期"
            } else {
                "暂未开通会员"
            }
        }

        mViewBinding.ivMineMemberVip.visibility =
            if (position == 1) View.VISIBLE else View.GONE
    }
}